import logging

from aioredis import from_url

from src.config import CONF

logger = logging.getLogger(__name__)


def init_redis(config):
    try:
        client = from_url(f'redis://{config.get("host")}:{config.get("port")}',
                          username=config.get('username'),
                          password=config.get('password'),
                          db=config.get('db'))
    except Exception as e:
        logger.error(f'redis initial failed with err:{str(e)}')
        client = None
    else:
        logger.info(f'redis initialized. host:{config.get("host")}, port:{config.get("port")}')
    return client
